/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.InteractionOperatorKind;


/**
 * @author ymortier <yann.mortier@obeo.fr>
 */
public class InteractionOperatorChoiceSection extends com.cea.papyrus.core.tabbedproperties.PropertyViewSection {

    /**
     * The stereotype appearance.
     */
    private CLabel interactionOperator;

    /**
     * The combo stereotype appearance.
     */
    private CCombo comboInteractionOperator;

    /**
     * The combo stereotype appearance listener.
     */
    private SelectionListener comboInteractionOperatorListener;

    /**
     * Creates the controls.
     * 
     * @param tabbedPropertySheetPage
     *            the tabbed property sheet page
     * @param parent
     *            the parent
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);
        Composite composite = getWidgetFactory().createFlatFormComposite(parent);
        FormData data;

        comboInteractionOperator = getWidgetFactory().createCCombo(composite, SWT.BORDER);
        for (InteractionOperatorKind interactionOperatorKind : InteractionOperatorKind.values()) {
            comboInteractionOperator.add(interactionOperatorKind.getName());
        }

        data = new FormData();
        data.left = new FormAttachment(0, 135);
        data.top = new FormAttachment(0, 0);
        comboInteractionOperator.setLayoutData(data);

        comboInteractionOperatorListener = new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                if (getElement() instanceof CombinedFragment) {
                    ((CombinedFragment) getElement())
                            .setInteractionOperator(InteractionOperatorKind
                                    .getByName(comboInteractionOperator
                                            .getItem(comboInteractionOperator.getSelectionIndex())));
                }
                refresh();
            }

            public void widgetDefaultSelected(SelectionEvent e) {
            }
        };
        comboInteractionOperator.addSelectionListener(comboInteractionOperatorListener);

        interactionOperator = getWidgetFactory().createCLabel(composite, "Interaction Operator:"); //$NON-NLS-1$
        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(comboInteractionOperator, 0);
        data.top = new FormAttachment(comboInteractionOperator, 1, SWT.CENTER);
        interactionOperator.setLayoutData(data);
    }

    /*
     * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
     */
    /**
     * Refresh.
     */
    @Override
    public void refresh() {
        if ((!comboInteractionOperator.isDisposed())) {

            comboInteractionOperator.removeSelectionListener(comboInteractionOperatorListener);

            if (getElement() instanceof CombinedFragment) {
                comboInteractionOperator.setText(((CombinedFragment) getElement())
                        .getInteractionOperator().getName());
            }

            comboInteractionOperator.addSelectionListener(comboInteractionOperatorListener);

        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
     */
    /**
     * 
     */
    public void dispose() {
        super.dispose();
        if (comboInteractionOperator != null && !comboInteractionOperator.isDisposed())
            comboInteractionOperator.removeSelectionListener(comboInteractionOperatorListener);
    }

}
